package com.tran.app.screen

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.tran.app.viewmodel.PlainViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CreatePlain(modifier: Modifier, navController: NavController,viewModel: PlainViewModel = PlainViewModel(),
                id:Int?=-1){
    viewModel.updateId(id)
    val uidata = viewModel.data.collectAsState()
    Scaffold(modifier = modifier.fillMaxSize(), topBar = {
        TopAppBar( title = { Text(text = "计划") },
            navigationIcon = {
                IconButton(onClick = { navController.popBackStack() }) {
                    Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "back")
                }
            })
    }) {
            Column(modifier = Modifier.padding(it)) {
                Row(modifier = Modifier.fillMaxWidth()
                , verticalAlignment = Alignment.CenterVertically) {
                    Text(text = "目的地", modifier = Modifier.padding(start = 20.dp).width(100.dp))
                    OutlinedTextField(modifier = Modifier.padding(start = 20.dp),label = {
                        Text(text = "目的地")
                    }, value = uidata.value.desplace, onValueChange = {
                        viewModel.data.value = viewModel.data.value.copy(
                            desplace = it
                        )
                    })
                }
                Row(modifier = Modifier
                    .padding(top = 10.dp)
                    .fillMaxWidth()
                    , verticalAlignment = Alignment.CenterVertically) {
                    Text(text = "出发时间", modifier = Modifier.padding(start = 20.dp).width(100.dp))
                    OutlinedTextField(modifier = Modifier.padding(start = 20.dp),label = {
                        Text(text = "出发时间")
                    }, value = uidata.value.starttime, onValueChange = {
                        viewModel.data.value = viewModel.data.value.copy(
                            starttime = it
                        )
                    })
                }
                Row(modifier = Modifier
                    .padding(top = 10.dp)
                    .fillMaxWidth()
                    , verticalAlignment = Alignment.CenterVertically) {
                    Text(text = "返回时间", modifier = Modifier.padding(start = 20.dp).width(100.dp))
                    OutlinedTextField(modifier = Modifier.padding(start = 20.dp), label = {
                        Text(text = "返回时间")
                    }, value = uidata.value.backtime, onValueChange = {
                        viewModel.data.value = viewModel.data.value.copy(
                            backtime = it
                        )
                    })
                }
                Row(modifier = Modifier
                    .padding(top = 10.dp)
                    .fillMaxWidth()
                    , verticalAlignment = Alignment.CenterVertically) {
                    Text(text = "预算", modifier = Modifier.padding(start = 20.dp).width(100.dp))
                    OutlinedTextField(modifier = Modifier.padding(start = 20.dp),label = {
                        Text(text = "预算")
                    }, value = uidata.value.money, onValueChange = {
                        viewModel.data.value = viewModel.data.value.copy(
                            money = it
                        )
                    })
                }
                Row(modifier = Modifier
                    .padding(top = 10.dp)
                    .fillMaxWidth()
                    , verticalAlignment = Alignment.CenterVertically) {
                    Text(text = "活动", modifier = Modifier.padding(start = 20.dp).width(100.dp))
                    OutlinedTextField(modifier = Modifier.padding(start = 20.dp),label = {
                        Text(text = "活动")
                    }, value = uidata.value.plain, onValueChange = {
                        viewModel.data.value = viewModel.data.value.copy(
                            plain = it
                        )
                    })
                }
                Row(modifier = Modifier.padding(30.dp).fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
                    Button(onClick = {
                                     viewModel.save()
                    },modifier=Modifier.width(200.dp)) {
                        Text(text = "保存")
                    }
                    Button(onClick = {
                                     navController.popBackStack()
                    }, modifier = Modifier.padding(start = 20.dp).width(200.dp)) {
                        Text(text = "取消")
                    }
                }
            }
    }
}